/**
 * 对用户提供一些操作配置文件的方法
 */
import fs from "fs";
 
import ConfigHandler from "./ConfigHandler";
import sugar from "../core/sugar";
import ControllerStructure from '../model/ControllerStructure';
import Middleware from '../middleware/middleware';
import ErrorUtils from '../utils/ErrorUtils';
import RouterHandler from '../route/RouterHandler';

export = class ConfigControl{
  // 添加路由文件
  public static addRouter(data:any):void{
    if(typeof data === "string"){
        ConfigHandler.addrouteConfig({path:sugar.path(data,{superNumber:3}),middleware:[],prefix:""})
    }else{
        // 初始化配置
        data.path = sugar.path(data.path,{superNumber:3});
        data.prefix || (data.prefix="")
        data.middleware || (data.middleware=[])
        ConfigHandler.addrouteConfig(data);
    }
  }

  // 添加控制器文件夹地址
  public static setController(str:string):void{
    ConfigHandler.addAddress("controller",sugar.path(str,{superNumber:3}));
  }

  // 设置中间件的文件夹地址
  public static setMiddleware(str:string):void{
    ConfigHandler.addAddress("middleware",sugar.path(str,{superNumber:3}));

  }

  // 设置注解的文件夹地址
  public static setAnnotation(str:string):void{
    ConfigHandler.addAddress("annotation",sugar.path(str,{superNumber:3}));
  }

  // 配置公共地址
  public static setPublic(path:string):void{
    ConfigHandler.addAddress("public",sugar.path(path,{superNumber:3}))
  }

  // 添加中间件
  public static addMiddleware(id:string,data:string|Function):void{
    if(typeof data === 'function'){
      return RouterHandler.addMiddleware(id,new ControllerStructure("",data,[]));
    }
    if(typeof data === 'string'){
      if(!/^[\/\\]|^[A-Z]:/.test(data)){
        if(ConfigHandler.getAddress("middleware")){
          let path = ConfigHandler.getAddress("middleware")+"/"+data;
          if(fs.existsSync(sugar.path(path))){
            data = path;
          }
        }        
      }
      ConfigHandler.addMiddleware(id,sugar.path(data));
      // 把中间件添加到中间件池
      return RouterHandler.addMiddleware(id,Middleware.auto_pars(id,data,id));
    }
    ErrorUtils.error("参数类型错误");
  }
  // 添加全局中间件
  public static addGlobalMiddleware(id:string|Function):void{
    if(typeof id === 'function'){
      return RouterHandler.addGlobalMiddleware(new ControllerStructure("",id,[]));
    }
    let data = ConfigHandler.getMiddleware(id);
    // 通过别称添加全局中间件
    if(data){
      ConfigHandler.addGlobal_Middleware(data);
      return RouterHandler.addGlobalMiddleware(RouterHandler.getMiddleware(id));
    }
    // 通过文件路径添加全局中间件配置
    ConfigHandler.addGlobal_Middleware(sugar.path(id));
    // 把中间件加入全局中间件池
    RouterHandler.addGlobalMiddleware(Middleware.pars_string_middleware(sugar.path(id),"",[]));
  }
  // 添加注解的运行地址
  public static addrunAnnotation(path:string):void{
    ConfigHandler.addrunAnnotation(sugar.path(path,{superNumber:3}));
  }

  // 添加一个注解
  public static addAnnotation(name:string,obj:any):void{
    ConfigHandler.addAnnotation(name,obj)
  }

  // 添加语法糖地址
  public static addSugar(name:string,path:string):void{
    ConfigHandler.addpathSugar(name,sugar.path(path,{superNumber:3}));
  }

  // 设置特性
  public static setFeatures(option:any):void{
    for(let key in option){
        ConfigHandler.setFeatures(key,option[key])
    }
  }
};